package app.server.project.main.service.db.entity.user;

import app.server.project.core.constants.Def;
import app.server.project.core.syndb.SynDBMgr;
import app.server.project.core.util.MathUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "user_info")
@TableName("user_info")
@Comment(value = "用户基本信息表")
public class UserInfoEntity {

    @Id
    @Column(name = "user_id")
    @Comment("用户主键")
    @TableId("user_id")
    private long userId;

    @TableField("lv")
    @Comment("等级")
    @Column(name = "lv")
    private int lv;

    @TableField("click_num")
    @Comment("点击次数")
    @Column(name = "click_num")
    private long clickNum;

    @TableField("start_click_num")
    @Comment("等级开始次数")
    @Column(name = "start_click_num")
    private long startClickNum;

    @TableField("end_click_num")
    @Comment("等级结束次数")
    @Column(name = "end_click_num")
    private long endClickNum;

    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    private LocalDateTime createTime;

    @TableField("energy")
    @Comment("体力值")
    @Column(name = "energy")
    private long energy;

    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    @TableField("ai_coin")
    @Comment("aiCoin")
    @Column(name = "ai_coin")
    private long aiCoin;

    @TableField("nick")
    @Comment("昵称")
    @Column(name = "nick")
    private String nick;

    @TableField("head_url")
    @Comment("头像")
    @Column(name = "head_url")
    private String headUrl;

    public static UserInfoEntity of(long userId) {
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setSynDb(true);
        userInfoEntity.setUserId(userId);
        userInfoEntity.setCreateTime(LocalDateTime.now());
        return userInfoEntity;
    }

    public void addClickNum(long num) {
        this.clickNum += num;
        this.setClickNum(this.clickNum);
    }

    public void addEnergyNum(long val) {
        if (this.energy == Long.MAX_VALUE) return;
        var temp = Long.MAX_VALUE - this.energy;
        if (temp >= val) {
            this.setEnergy(this.energy + val);
        } else {
            this.setEnergy(Long.MAX_VALUE);
        }
    }

    public void setAiCoin(long aiCoin) {
        this.aiCoin = aiCoin;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserInfoEntity::getAiCoin, this);
    }

    public void addAiCoin(long num) {
        var ret = MathUtil.canAddNum(this.aiCoin, num);
        this.aiCoin += ret;
        this.setAiCoin(aiCoin);
    }

    public void consumeAiCoin(long num) {
        if (num >= this.aiCoin) {
            this.setAiCoin(Def.Zero);
        } else {
            this.aiCoin -= num;
            this.setAiCoin(this.aiCoin);
        }


    }

    public void setEnergy(long energy) {
        this.energy = energy;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserInfoEntity::getEnergy, this);
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserInfoEntity::getCreateTime, this);
    }

    public void setLv(int lv) {
        this.lv = lv;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserInfoEntity::getLv, this);
    }

    public void setClickNum(long clickNum) {
        this.clickNum = clickNum;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserInfoEntity::getClickNum, this);
    }

    public void setStartClickNum(long startClickNum) {
        this.startClickNum = startClickNum;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserInfoEntity::getStartClickNum, this);
    }

    public void setNick(String nick) {
        this.nick = nick;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserInfoEntity::getNick, this);
    }

    public void setHeadUrl(String headUrl) {
        this.headUrl = headUrl;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserInfoEntity::getHeadUrl, this);
    }

    public void setEndClickNum(long endClickNum) {
        this.endClickNum = endClickNum;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserInfoEntity::getEndClickNum, this);
    }
}
